import { _decorator, Component, Button, ProgressBar, Node, log, find, instantiate, Prefab, SpriteFrame, AssetManager, assetManager, Texture2D, ImageAsset, director, tween, sys } from 'cc';
const { ccclass, property } = _decorator;

import { Consts } from "../Data/Consts";
import { GameData } from "../Data/GameData";
import GameStorage from "../Data/GameStorage";
import SS_UIManager from "../Manager/SS_UIManager";
import CeBianLanPanel from "./CeBianLanPanel";
import MaterialPanel from "./MaterialPanel";
import ProductPanel from "./ProductPanel";
import TAComponent from '../sdk/TAComponent';
import { SS_AudioManager } from '../Manager/SS_AudioManager';
import CommonFunction from '../utils/CommonFunction';
import { Lockable } from '../Common/Lockable';

@ccclass('StartPanel')
export default class StartPanel extends Component {
    public static Instance: StartPanel;
    @property(Button)
    Button_startGame: Button | null = null;
    @property(Button)
    Button_item: Button | null = null;
    @property(Button)
    Button_product: Button | null = null;
    @property(Button)
    Button_侧边栏: Button | null = null;
    @property(ProgressBar)
    ProgressBar: ProgressBar | null = null;
    @property(Node)
    main: Node | null = null;
    @property(Node)
    title: Node | null = null;

    onLoad() {
        StartPanel.Instance = this;
    }

    loading() {
        this.ProgressBar.progress = 0;
        tween(this.ProgressBar)
            .to(4, { progress: 1 })
            .call(() => {
            })
            .start();
    }

    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();
    }

    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }

    async Refresh_title() {
        CommonFunction.SetAllChildrenAtive(this.title, false);

        const isKSGame = typeof window['KSGameGlobal'] != 'undefined';
        // 抖音平台
        if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
            find("tt", this.title).active = true;
        }
        // 快手平台
        else if (isKSGame) {
            find("ks", this.title).active = true;
        }
        // 微信平台
        else if (sys.platform == sys.Platform.WECHAT_GAME) {
            find("wx", this.title).active = true;
        }
        else {
            find("browser", this.title).active = true;
        }
    }

    async Refresh() {
        if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
            this.Button_侧边栏.node.active = true;
        }
        else {
            this.Button_侧边栏.node.active = false;
        }

        this.Refresh_title();
    }

    AddListener() {
        this.Button_startGame.node.on("click", this.On_Button_startGame, this);
        this.Button_item.node.on("click", this.On_Button_item, this);
        this.Button_product.node.on("click", this.On_Button_product, this);
        this.Button_侧边栏.node.on("click", this.On_Button_侧边栏, this);
    }
    RemoveListener() {
        this.Button_startGame.node.off("click", this.On_Button_startGame, this);
        this.Button_item.node.off("click", this.On_Button_item, this);
        this.Button_product.node.off("click", this.On_Button_product, this);
        this.Button_侧边栏.node.off("click", this.On_Button_侧边栏, this);
    }

    @Lockable(1000)
    async On_Button_startGame() {
        this.ShowOff();

        this.进入管卡();

        TAComponent.SendEvent(Consts.埋点事件.点击开始游戏, {});

        SS_AudioManager.PlayButtonAudio();
    }

    进入管卡() {
        GameData.CurrentLevel = GameStorage.data.CurrentLevel;
        director.loadScene("GameScene");
    }

    @Lockable(1000)
    async On_Button_item() {
        SS_UIManager.Instance.CreatePanel(`UI/MaterialPanel`, MaterialPanel);

        SS_AudioManager.PlayButtonAudio();
    }

    @Lockable(1000)
    async On_Button_product() {
        SS_UIManager.Instance.CreatePanel(`UI/ProductPanel`, ProductPanel);

        SS_AudioManager.PlayButtonAudio();
    }

    @Lockable(1000)
    async On_Button_侧边栏() {
        SS_UIManager.Instance.CreatePanel(`UI/CeBianLanPanel`, CeBianLanPanel);

        SS_AudioManager.PlayButtonAudio();
    }
}